package ac.github.os.internal.core.action.location.particle

import ac.github.os.OriginSkill
import ac.github.os.api.OriginSkillAPI
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.core.objective.EntityObjective
import ac.github.os.internal.core.objective.LocationObjective
import ac.github.os.internal.session.Cache
import ac.github.os.internal.session.Session
import taboolib.common.platform.Awake
import taboolib.common.platform.function.submit
import taboolib.module.effect.Line


@Awake
class LineParticle : IParticleAction() {
    override val particleName: String
        get() = "line"

    override fun onExecute(session: Session, cache: Cache, config: StringConfig) {
        val step = config.step()

        val particleSpawner = config.spawner()

        val target = config.get<String>("target") ?: return
        val arrayListOf = mutableListOf<EntityObjective>()
        val c = Cache()
        target.split(" ").forEach {
            OriginSkillAPI.check(it).apply {
                val substring = key.substring(1)
                value.initial(session)
                OriginSkill.selectors.firstOrNull { it.namespace == substring }?.doSelect(session, value)?.forEach {
                    cache.objectives.add(it)
                }
            }
        }
        arrayListOf.addAll(c.objectives.filterIsInstance<EntityObjective>())

        submit(async = true) {
            cache.objectives.filterIsInstance<LocationObjective>().forEach {
                arrayListOf.forEach { entityObjective ->
                    Line(
                        it.location.adapt(),
                        entityObjective.location.adapt(),
                        step,
                        particleSpawner
                    ).inject(config).show()
                }
            }
        }
    }
}